package com.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class App implements Runnable {

	private JFrame frame;

	private JButton start;

	private JButton stop;

	private JLabel name;

	private JPanel jPanel;

	private Font font;

	private String[] names;

	private boolean flag = true;// 线程的总开关

	private boolean power = false;// 线程暂停的开关

	public App(String[] names) {
		super();

		this.names = names;

		frame = new JFrame("幸运的你！");

		frame.setSize(800, 700);

		frame.getContentPane().setBackground(Color.red);

		start = new JButton("开始");

		stop = new JButton("停止");

		stop.setVisible(false);

		Thread thread = new Thread(this);

		thread.start();

		start.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				power = true;
				start.setVisible(false);
				stop.setVisible(true);

			}
		});

		stop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				power = false;
				name.setText("恭喜 " + name.getText() + " 中奖");
				start.setVisible(true);
				stop.setVisible(false);
			}

		});

		font = new Font("楷体", Font.BOLD, 100);

		name = new JLabel("谁会这么幸运?", JLabel.CENTER);

		name.setForeground(Color.yellow);

		name.setFont(font);

		frame.setBackground(Color.red);

		frame.setLocationRelativeTo(null);

		frame.setLayout(new BorderLayout());

		frame.add(name, BorderLayout.CENTER);

		jPanel = new JPanel();

		jPanel.add(start);
		jPanel.add(stop);

		frame.add(jPanel, BorderLayout.SOUTH);

		frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame);

		frame.setVisible(true);

	}

	@Override
	public void run() {
		while (flag) {
			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(power){
				int number = (int) (Math.random() * (names.length*100));

				name.setText(names[number/100]);
			}
		}

	}

}
